package cn.cocho.android.mc.net;

import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import cn.cocho.android.mc.storage.ApplicationCache;

/**
 * 网络操作工具类,具有判断网络是否连接等
 * 
 * @author hsx
 * @time 2013-4-25下午7:06:50
 */
public class NetUtils {

	/**
	 * 根据指定的URL判断网络是否可用, 默认超时时长为3秒
	 * 
	 * @param url
	 * @return
	 * @author HAIKANG SONG
	 */
	public static boolean netAvailable(String url) {
		return netAvailable(url, 3000);
	}

	/**
	 * 根据指定的URL判断网络是否可用
	 * 
	 * @param url
	 *            指定的URL路径
	 * @param timeOut
	 *            请求超时时长
	 * @return
	 * @author HAIKANG SONG
	 */
	public static boolean netAvailable(String url, int timeOut) {
		boolean result = true;
		int REQUEST_TIMEOUT = 3 * 1000;// 设置请求超时3秒钟
		try {
			URL u = new URL(url);
			HttpURLConnection connection = (HttpURLConnection) u.openConnection();
			connection.setConnectTimeout(REQUEST_TIMEOUT);
			connection.getInputStream();
		} catch (Exception e) {
			result = false;
		}
		return result;
	}

	/**
	 * 判断网络是否连接
	 * 
	 * @return
	 * @return boolean
	 * @author hsx
	 * @time 2013-4-25上午11:37:09
	 */
	public static boolean isNetworkConnected() {
		ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationCache.context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mNetworkInfo = connectivityManager.getActiveNetworkInfo();
		if (mNetworkInfo != null) {
			return mNetworkInfo.isAvailable();
		}
		return false;
	}

	/**
	 * 判断wifi是否连接
	 * 
	 * @return
	 * @return boolean
	 * @author hsx
	 * @time 2013-4-25上午11:42:11
	 */
	public static boolean isWifiConnected() {
		ConnectivityManager mConnectivityManager = (ConnectivityManager) ApplicationCache.context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (mWiFiNetworkInfo != null) {
			return mWiFiNetworkInfo.isConnectedOrConnecting();
		}
		return false;
	}

	/**
	 * 
	 * 判断手机网络是否可用
	 * 
	 * @return
	 * @return boolean
	 * @author hsx
	 * @time 2013-4-25上午11:43:28
	 */
	public static boolean isMobileAvailable() {
		ConnectivityManager mConnectivityManager = (ConnectivityManager) ApplicationCache.context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if (mMobileNetworkInfo != null) {
			return mMobileNetworkInfo.isConnectedOrConnecting();
		}
		return false;
	}

	/***
	 * 
	 * 获取网络的连接类型
	 * 
	 * @param context
	 * @return NO_NET:无网络，或者 WIFI:WIFI网，MOBILE：MOBILE网络等
	 * @return int
	 * @author hsx
	 * @time 2013-4-25上午11:45:07
	 */
	public static String getConnectedType() {
		ConnectivityManager mConnectivityManager = (ConnectivityManager) ApplicationCache.context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
		if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
			return mNetworkInfo.getTypeName();
		}
		return "NO_NET";
	}

}
